package com.shop.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.shop.bean.Admin;
import com.shop.dao.BaseDao;
import com.shop.dao.LoginDao;

public class LoginDaoImpl implements LoginDao{
	public Admin queryByAP(String account, String password) {
		Connection conn = BaseDao.getConn();
		PreparedStatement s = null;
		ResultSet rs = null;
		try {
			String sql = "SELECT * FROM admin WHERE account = ? and password = ?";
			s = conn.prepareStatement(sql);
			s.setString(1, account);
			s.setString(2, password);
			rs = s.executeQuery();
			while (rs.next()) {
				Admin a = new Admin();
				a.setId(rs.getInt("id"));
				a.setAccount(rs.getString("account"));
				a.setPassword(rs.getString("password"));
				return a;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(conn, s, rs);
		}
		return null;
	}

	public Admin queryByAccount(String account) {
		Connection conn = BaseDao.getConn();
		PreparedStatement s = null;
		ResultSet rs = null;
		try {
			String sql = "SELECT * FROM admin WHERE account = ?";
			s = conn.prepareStatement(sql);
			s.setString(1, account);
			rs = s.executeQuery();
			while (rs.next()) {
				Admin a = new Admin();
				a.setId(rs.getInt("id"));
				a.setAccount(rs.getString("account"));
				a.setPassword(rs.getString("password"));
				return a;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(conn, s, rs);
		}
		return null;
	}
}
